package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Verification;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.mapper.VerificationMapper;
import cn.wolfcode.trip.base.service.IVerificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VerificationServiceImpl implements IVerificationService {

    @Autowired
    private VerificationMapper codeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void saveOrUpdate(Verification code) {
        Verification v = codeMapper.selectCodeByUserId(code.getUserId());
        if (v == null){
            codeMapper.insert(code);
        } else {
            codeMapper.update(code);
        }
    }

    @Override
    public Verification selectCodeByUserId(Long userId) {
        return codeMapper.selectCodeByUserId(userId);
    }

    @Override
    public Verification checkCode(int code,String password) {
        Verification v = codeMapper.selectCode(code);
        System.out.println(v.toString());
        if (v == null){
            throw new RuntimeException("验证码不正确");
        } else {
            userMapper.updatePassword(v.getUserId(),password);
            return v;
        }
    }
}
